<template>
  <div class="bg">
  <div class="login-container">
    <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position="left" label-width="0px" class="login-form">
      <h3 class="title">高校教师成果管理系统</h3>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" autocomplete="off" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" autocomplete="off" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item prop="role">
        <el-select v-model="loginForm.role"  style="width: 100%;" placeholder="请选择角色">
          <el-option label="系统管理员" :value="1"></el-option>
        <el-option label="成果管理员" :value="2"></el-option>
        <el-option label="教师" :value="3"></el-option>
     
        </el-select>
      </el-form-item>

      <el-form-item style="width: 100%;">
        <el-button type="primary" style="width: 100%;" @click="handleLogin">登录</el-button>
      </el-form-item>
      <div class="tip" @click="register()">注册</div>
    </el-form>
  </div>
</div>
</template>

<script>
import service from '@/utils/request';

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    register(){
      this.$router.push({
            path:"/register"
        })
    },
    handleLogin() {
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {

          const res=await service.post("/auth/login",this.loginForm);
          console.log(res)
          if(res.code == 200){
            window.localStorage.setItem("userinfo",JSON.stringify(res.data.userinfo))
            window.localStorage.setItem("token",res.data.token)
            const ress = await service.get("/auth/info");
            console.log(ress)
            this.$message.success("登录成功");
            if(ress.data.roles&&ress.data.roles[0] == 'ROOT'){
              this.$router.push({
              path:"/system"
            })
            }
            else if(ress.data.roles&&ress.data.roles[0] == 'ACHIEVEMENT'){
              this.$router.push({
              path:"/product"
            })
            }
            
            else if(ress.data.roles&&ress.data.roles[0] == 'TEACHER'){
              this.$router.push({
              path:"/teacher"
            })
            }

        

          }
        } else {
          console.log('表单校验失败');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.tip{
  color: #2a60c9;
  font-size: 14px;
  cursor: pointer;
text-align: right;
}
.login-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  color: #333;
}

.login-form {
  width: 100%;
}

.el-input {
  width: 100%;
}
.bg{
  width: 100%;
  height: 100%;
  background: url("../../assets/cg.png") no-repeat ;
  background-size: 100% 100%;

}
</style>
